package cn.maszy.action.projectmanagement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sourceforge.stripes.action.SessionScope;
import org.springframework.beans.factory.annotation.Autowired;
import cn.maszy.common.BaseAction;
import cn.maszy.common.DateTools;
import cn.maszy.common.Pager;
import cn.maszy.common.SessionFactory;
import cn.maszy.common.Transcoding;
import cn.maszy.model.projectmanagement.B_T_Ufp_EngineerCategory;
import cn.maszy.model.projectmanagement.B_T_Ufp_EngineerMaterial;
import cn.maszy.model.projectmanagement.B_T_Ufp_Engineering;
import cn.maszy.model.systemsetting.B_T_Ufp_Material;
import cn.maszy.model.systemsetting.B_T_Ufp_Project;
import cn.maszy.service.projectmanagement.B_T_Ufp_EngineerMaterialService;
import cn.maszy.service.projectmanagement.B_T_Ufp_EngineeringService;
import cn.maszy.service.systemsetting.B_T_Ufp_MaterialService;

@SessionScope
public class EngineerMaterialAction extends BaseAction<B_T_Ufp_EngineerMaterial> {
	/**
	 * 版本控制信息
	 */
	private static final long serialVersionUID = 6179604301571294802L;
	private B_T_Ufp_Material b_T_Ufp_Material;
	private B_T_Ufp_EngineerMaterial b_T_Ufp_EngineerMaterial;
	//自定义项目添加的时候用
	private B_T_Ufp_EngineerMaterial b_T_Ufp_EngineerMaterialCustom;
	@Autowired
	private B_T_Ufp_MaterialService b_T_Ufp_MaterialService;
	@Autowired
	private B_T_Ufp_EngineerMaterialService b_T_Ufp_EngineerMaterialService;
	@Autowired
	private B_T_Ufp_EngineeringService b_T_Ufp_EngineeringService;
	private String checkIds;
	private String status;
	private String categoryStatus;
	//总金额
	private Float totalamount;
	//用于传递我的工程时 给具体部位增加数据时，传递uuid和那么给他的子类。
	private String positiontype;
	private List<B_T_Ufp_Material> list_b_T_Ufp_Material;
	private JSONArray data;
	//是否可以修改
	private String isupdate;
	/*******************STRUTS ACTION METHOD*************************/
	//删除session中的数据
	public String engineerMaterialRemove()throws Exception
	{
		list_b_T_Ufp_Material = (List<B_T_Ufp_Material>) request.getSession().getAttribute("Materialitem");
		if(list_b_T_Ufp_Material!=null)
		{
			for(int i=list_b_T_Ufp_Material.size();i>0;i--)
			{
				if(list_b_T_Ufp_Material.get(i-1).getMaterialName().equals(checkIds))
				{
					list_b_T_Ufp_Material.remove(i-1);
				}
			}
		}
		return "success";
	}
	 //获取session值并传递到页面js中进行设置
	public String getsesssion() throws Exception{
		list_b_T_Ufp_Material = (List<B_T_Ufp_Material>) request.getSession().getAttribute("Materialitem");
		if(list_b_T_Ufp_Material!=null)
		{
			data = JSONArray.fromObject(list_b_T_Ufp_Material); 
		}
		return SUCCESS;
	}
	public String engineerMaterialsetsession()throws Exception{
		if(b_T_Ufp_Material!=null)
		{			
			list_b_T_Ufp_Material = (List<B_T_Ufp_Material>) request.getSession().getAttribute("Materialitem");
			if(list_b_T_Ufp_Material ==null){
				list_b_T_Ufp_Material = new ArrayList<B_T_Ufp_Material>();
			}
				//添加材料的时候不能重复添加
				//这里讲materialname字段当做materialnum字段用。
				for(int i=0;i<list_b_T_Ufp_Material.size();i++)
				{
					if(b_T_Ufp_Material.getMaterialName().equals(list_b_T_Ufp_Material.get(i).getMaterialName()))
					{	//先请除session
						request.getSession().removeAttribute("Materialitem");
						int j =list_b_T_Ufp_Material.get(i).getQuantity()+b_T_Ufp_Material.getQuantity();
						float k =list_b_T_Ufp_Material.get(i).getTotalAmount()+b_T_Ufp_Material.getQuantity()*b_T_Ufp_Material.getAngebotspreis();
						list_b_T_Ufp_Material.get(i).setQuantity(j);
						list_b_T_Ufp_Material.get(i).setTotalAmount(k);
						
						//再把设置过的值放入session
						request.getSession().setAttribute("Materialitem", list_b_T_Ufp_Material);
						return ajaxForwardSuccess(getText("msg.operation.success"), "","materialRecycling", "closeCurrent", "");
					}
				}
				
				if(positiontype!=null&&!"".equals(positiontype))
				{
					positiontype =Transcoding.getcode(request, positiontype);
//					positiontype =new String(positiontype.getBytes("ISO8859_1"),"GBK");
					//这里占时借用这个字段来存储工程uuid
					b_T_Ufp_Material.setCategoryUuid(positiontype);
				}
				float s =b_T_Ufp_Material.getAngebotspreis()*b_T_Ufp_Material.getQuantity();
				b_T_Ufp_Material.setTotalAmount(s);
				list_b_T_Ufp_Material.add(b_T_Ufp_Material);
				request.getSession().setAttribute("Materialitem", list_b_T_Ufp_Material);
		}
		
		return ajaxForwardSuccess(getText("msg.operation.success"), "","materialRecycling", "closeCurrent", "");
	}
	public String engineerMaterialDelete() throws Exception {
		int i=b_T_Ufp_EngineerMaterialService.delete(checkIds);
		if(i>0)
		return ajaxForwardSuccess(getText("msg.operation.success"), "","engineerCategoryList", "", "");
		return ajaxForwardError(getText("msg.operation.failure"));
	}
	public String engineerMaterialUpdate() throws Exception {
		//保存时获取session中设置的值，
		list_b_T_Ufp_Material = (List<B_T_Ufp_Material>) request.getSession().getAttribute("Materialitem");
				if(list_b_T_Ufp_Material!=null)
				{
					int j=0;
					for(int i=0;i<list_b_T_Ufp_Material.size();i++)
					{
						b_T_Ufp_Material=list_b_T_Ufp_Material.get(i);
						b_T_Ufp_EngineerMaterial = new B_T_Ufp_EngineerMaterial();
						b_T_Ufp_EngineerMaterial.setEngineeringUuid(b_T_Ufp_Material.getCategoryUuid());
						b_T_Ufp_EngineerMaterial.setEngineeringName(b_T_Ufp_Material.getCategoryName());
						b_T_Ufp_EngineerMaterial.setMaterialName(b_T_Ufp_Material.getMaterialName());
						b_T_Ufp_EngineerMaterial.setUnit(b_T_Ufp_Material.getUnit());
						b_T_Ufp_EngineerMaterial.setMaterialModel(b_T_Ufp_Material.getMaterialModel());
						b_T_Ufp_EngineerMaterial.setMaterialBrand(b_T_Ufp_Material.getMaterialBrand());
						b_T_Ufp_EngineerMaterial.setFinishes(b_T_Ufp_Material.getFinishes());
						b_T_Ufp_EngineerMaterial.setProducter(b_T_Ufp_Material.getProducter());
						b_T_Ufp_EngineerMaterial.setAngebotspreis(b_T_Ufp_Material.getAngebotspreis());
						b_T_Ufp_EngineerMaterial.setQuantity(b_T_Ufp_Material.getQuantity());
						b_T_Ufp_EngineerMaterial.setRemark(b_T_Ufp_Material.getRemark());
						b_T_Ufp_EngineerMaterial.setEngineeringUuid(b_T_Ufp_Material.getCategoryUuid());
						float s =b_T_Ufp_Material.getQuantity()*b_T_Ufp_Material.getAngebotspreis();
						b_T_Ufp_EngineerMaterial.setTotalAmount(s);
						j=b_T_Ufp_EngineerMaterialService.update(b_T_Ufp_EngineerMaterial);
						if(j>0)
						{
							//查询 这条数据所属的工程 的项目金额总和
							B_T_Ufp_EngineerMaterial b_T_Ufp_EngineerMaterial1 = new B_T_Ufp_EngineerMaterial();
							b_T_Ufp_EngineerMaterial1.setEngineeringUuid(b_T_Ufp_Material.getCategoryUuid());
							float price =b_T_Ufp_EngineerMaterialService.totalamount(b_T_Ufp_EngineerMaterial1);
							//设置参数并修改工程 的项目总价格
							B_T_Ufp_Engineering b_T_Ufp_Engineering=new B_T_Ufp_Engineering();
							b_T_Ufp_Engineering.setUuid(b_T_Ufp_Material.getCategoryUuid());
							b_T_Ufp_Engineering.setMaterialPrice(price);
							b_T_Ufp_EngineeringService.update(b_T_Ufp_Engineering);
						}
					}
					if(j>0)
					//目前只有点击提交后关闭
						//保存成功清除session
					request.getSession().removeAttribute("Materialitem");
					return ajaxForwardSuccess(getText("msg.operation.success"), "","engineerCategoryList", "closeCurrent", "");
				}
				else
				{
					if(b_T_Ufp_EngineerMaterial ==null)
					{
						return ajaxForwardError(getText("请填写数据后保存"));
					}
					int i=b_T_Ufp_EngineerMaterialService.update(b_T_Ufp_EngineerMaterial);
					if(i>0)
					{	
						float price =b_T_Ufp_EngineerMaterialService.totalamount(b_T_Ufp_EngineerMaterial);
						//设置参数并修改工程 的项目总价格
						B_T_Ufp_Engineering b_T_Ufp_Engineering=new B_T_Ufp_Engineering();
						b_T_Ufp_Engineering.setUuid(b_T_Ufp_EngineerMaterial.getEngineeringUuid());
						b_T_Ufp_Engineering.setMaterialPrice(price);
						b_T_Ufp_EngineeringService.update(b_T_Ufp_Engineering);
						return ajaxForwardSuccess(getText("msg.operation.success"), "","engineerCategoryList", "closeCurrent", "");
					}
					if(i>0)
					return ajaxForwardSuccess(getText("msg.operation.success"), "","engineerCategoryList", "closeCurrent", "");
				}
		return ajaxForwardError(getText("msg.operation.failure"));
	}
	public String engineerMaterialList() throws Exception {
		//查询所属部位的合计金额
		totalamount = b_T_Ufp_EngineerMaterialService.totalamount(b_T_Ufp_EngineerMaterial);
		
		pager = new Pager<B_T_Ufp_EngineerMaterial>(pageNum, numPerPage,orderField,orderDirection);
		pager= b_T_Ufp_EngineerMaterialService.selectList(b_T_Ufp_EngineerMaterial,pager);
		return SUCCESS;
	}
	public String engineerMaterialDetail() throws Exception {
		//当添加工程量的时候调。
		if(b_T_Ufp_Material!=null)
		{
			b_T_Ufp_Material =b_T_Ufp_MaterialService.selectOne(b_T_Ufp_Material.getUuid());
			return SUCCESS;
		}
		if(b_T_Ufp_EngineerMaterial==null){
			b_T_Ufp_EngineerMaterial=b_T_Ufp_EngineerMaterialService.selectOne("");
			
		}else{
			// 自定义添加项目首先记住传过来的工程uuid
			String engineeringuuid = b_T_Ufp_EngineerMaterial.getEngineeringUuid();
			
			b_T_Ufp_EngineerMaterial=b_T_Ufp_EngineerMaterialService.selectOne(b_T_Ufp_EngineerMaterial.getUuid());
			
			if(engineeringuuid!=null&&!"".equals(engineeringuuid))
			{
				b_T_Ufp_EngineerMaterialCustom = new B_T_Ufp_EngineerMaterial();
				//自动材料编码
				String str = new SimpleDateFormat("MMddHHmmss").format(new java.util.Date());
				String code ="XM_"+str+SessionFactory.getSessionUserName();
				b_T_Ufp_EngineerMaterialCustom.setMaterialName(code);
				b_T_Ufp_EngineerMaterialCustom.setEngineeringUuid(engineeringuuid);
			}
		}
		return SUCCESS;
	}
	
/********************* class set/get ****************************/
	
	public B_T_Ufp_EngineerMaterial getB_T_Ufp_EngineerMaterial() {
		return b_T_Ufp_EngineerMaterial;
	}

	public void setB_T_Ufp_EngineerMaterial(B_T_Ufp_EngineerMaterial b_T_Ufp_EngineerMaterial) {
		this.b_T_Ufp_EngineerMaterial = b_T_Ufp_EngineerMaterial;
	}
	
	public String getCheckIds() {
		return checkIds;
	}

	public void setCheckIds(String checkIds) {
		this.checkIds = checkIds;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	public String getCategoryStatus() {
		return categoryStatus;
	}
	public void setCategoryStatus(String categoryStatus) {
		this.categoryStatus = categoryStatus;
	}
	public B_T_Ufp_Material getB_T_Ufp_Material() {
		return b_T_Ufp_Material;
	}
	public void setB_T_Ufp_Material(B_T_Ufp_Material b_T_Ufp_Material) {
		this.b_T_Ufp_Material = b_T_Ufp_Material;
	}
	public String getPositiontype() {
		return positiontype;
	}
	public void setPositiontype(String positiontype) {
		this.positiontype = positiontype;
	}
	public Float getTotalamount() {
		return totalamount;
	}
	public void setTotalamount(Float totalamount) {
		this.totalamount = totalamount;
	}
	public List<B_T_Ufp_Material> getList_b_T_Ufp_Material() {
		return list_b_T_Ufp_Material;
	}
	public void setList_b_T_Ufp_Material(
			List<B_T_Ufp_Material> list_b_T_Ufp_Material) {
		this.list_b_T_Ufp_Material = list_b_T_Ufp_Material;
	}
	public JSONArray getData() {
		return data;
	}
	public void setData(JSONArray data) {
		this.data = data;
	}
	public B_T_Ufp_EngineerMaterial getB_T_Ufp_EngineerMaterialCustom() {
		return b_T_Ufp_EngineerMaterialCustom;
	}
	public void setB_T_Ufp_EngineerMaterialCustom(
			B_T_Ufp_EngineerMaterial b_T_Ufp_EngineerMaterialCustom) {
		this.b_T_Ufp_EngineerMaterialCustom = b_T_Ufp_EngineerMaterialCustom;
	}
	public String getIsupdate() {
		return isupdate;
	}
	public void setIsupdate(String isupdate) {
		this.isupdate = isupdate;
	}
}
